
package com.kw.common.ocr.service;

import com.aliyun.ocr20191230.Client;
import com.aliyun.ocr20191230.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.kw.common.ocr.OcrProperties;
import com.kw.common.ocr.dto.BankCardDto;
import com.kw.common.ocr.dto.BusinessLicenceDto;
import com.kw.common.ocr.dto.IdCardBackDto;
import com.kw.common.ocr.dto.IdCardFaceDto;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;


@RequiredArgsConstructor
public class AliyumOcrTemplate implements InitializingBean,OcrTemplate {

	private final OcrProperties ocrProperties;

	private Client client;

	@Override
	public void afterPropertiesSet() throws Exception {
		Config config = new Config()
			// 您的AccessKey ID
			.setAccessKeyId(ocrProperties.getAccessKey())
			// 您的AccessKey Secret
			.setAccessKeySecret(ocrProperties.getSecretKey());
		// 访问的域名
		config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
		this.client = new Client(config);
	}

	//身份证正面
	public IdCardFaceDto faceScan(String imgUrl) throws Exception {
		RecognizeIdentityCardRequest recognizeIdentityCardRequest = new RecognizeIdentityCardRequest();
		recognizeIdentityCardRequest.setImageURL(imgUrl);
		recognizeIdentityCardRequest.setSide("face");//face
		RecognizeIdentityCardResponse resp = client.recognizeIdentityCard(recognizeIdentityCardRequest);
		IdCardFaceDto idcardFaceDto = new IdCardFaceDto();
		idcardFaceDto.setAddress(resp.body.getData().frontResult.getAddress());
		idcardFaceDto.setGender(resp.body.getData().frontResult.getGender());
		idcardFaceDto.setBirthDate(resp.body.getData().frontResult.getBirthDate());
		idcardFaceDto.setIdnumber(resp.body.getData().frontResult.getIDNumber());
		idcardFaceDto.setName(resp.body.getData().frontResult.getName());
		idcardFaceDto.setNationality(resp.body.getData().frontResult.getNationality());
		return idcardFaceDto;
	}
	//身份证反面
	public IdCardBackDto backScan(String imgUrl) throws Exception {
		RecognizeIdentityCardRequest recognizeIdentityCardRequest = new RecognizeIdentityCardRequest();
		recognizeIdentityCardRequest.setImageURL(imgUrl);
		recognizeIdentityCardRequest.setSide("back");//face
		RecognizeIdentityCardResponse resp = client.recognizeIdentityCard(recognizeIdentityCardRequest);
		IdCardBackDto idCardBackDto = new IdCardBackDto();
		idCardBackDto.setEndDate(resp.body.getData().backResult.getEndDate());
		idCardBackDto.setStartDate(resp.body.getData().backResult.getStartDate());
		idCardBackDto.setIssue(resp.body.getData().backResult.getIssue());
		return idCardBackDto;
	}
	//营业执照
	public BusinessLicenceDto businessLiscenceScan(String imgUrl) throws Exception {
		RecognizeBusinessLicenseRequest recognizeBusinessLicenseRequest = new RecognizeBusinessLicenseRequest();
		recognizeBusinessLicenseRequest.setImageURL(imgUrl);
		RecognizeBusinessLicenseResponse recognizeBusinessLicenseResponse = client.recognizeBusinessLicense(recognizeBusinessLicenseRequest);
		BusinessLicenceDto businessListenerDto = new BusinessLicenceDto();
		businessListenerDto.setAddress(recognizeBusinessLicenseResponse.body.data.getAddress());
		businessListenerDto.setValidPeriod(recognizeBusinessLicenseResponse.body.data.getValidPeriod());
		businessListenerDto.setCapital(recognizeBusinessLicenseResponse.body.data.getCapital());
		businessListenerDto.setLegalPerson(recognizeBusinessLicenseResponse.body.data.getLegalPerson());
		businessListenerDto.setEstablishDate(recognizeBusinessLicenseResponse.body.data.establishDate);
		businessListenerDto.setName(recognizeBusinessLicenseResponse.body.data.name);
		businessListenerDto.setRegisterNumber(recognizeBusinessLicenseResponse.body.data.registerNumber);
		businessListenerDto.setType(recognizeBusinessLicenseResponse.body.data.type);
		businessListenerDto.setBusiness(recognizeBusinessLicenseResponse.body.data.business);
		return businessListenerDto;
	}
	public BankCardDto bankCardScan(String imgUrl) throws Exception {
		RecognizeBankCardRequest recognizeBankCardRequest = new RecognizeBankCardRequest();
		recognizeBankCardRequest.setImageURL(imgUrl);
		// 复制代码运行请自行打印 API 的返回值
		RecognizeBankCardResponse recognizeBankCardResponse = client.recognizeBankCard(recognizeBankCardRequest);
		BankCardDto bankCardDto = new BankCardDto();
		bankCardDto.setBankName(recognizeBankCardResponse.body.data.bankName);
		bankCardDto.setCardNumber(recognizeBankCardResponse.body.data.cardNumber);
		bankCardDto.setValidDate(recognizeBankCardResponse.body.data.validDate);
		return bankCardDto;
	}




}
